package com.example.my.android;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.service.autofill.TextValueSanitizer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LocationActivity extends AppCompatActivity {

    private TextView longitudeText;
    private TextView latitudeText;
    private Button saveParkingButton;
    private Button viewButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        longitudeText =findViewById(R.id.longitude_text);
        latitudeText = findViewById(R.id.latitude_text);
        saveParkingButton = findViewById(R.id.save_parking_button);
        saveParkingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.dialog_parking, null);
                EditText titleEdit = view.findViewById(R.id.title_edit);

                new AlertDialog.Builder(v.getContext())
                        .setTitle("车位编号")
                        .setView(view)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String longitude = longitudeText.getText().toString();
                                String latitude = latitudeText.getText().toString();
                                String title = titleEdit.getText().toString();

                                Parking parking = new Parking();
                                parking.setTitle(title);
                                parking.setLongitude(longitude);
                                parking.setLatitude(latitude);

                                ParkingUtil parkingUtil = new ParkingUtil(v.getContext());
                                if (parkingUtil.save(parking)) {
                                    Toast.makeText(v.getContext(), "停车位信息保存成功！", Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(v.getContext(), "停车位信息保存失败！", Toast.LENGTH_LONG).show();
                                }
                            }
                        })
                        .show();
            }
        });
        viewButton = findViewById(R.id.view_button);
        viewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ParkingActivity.class);
                startActivity(intent);
            }
        });

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            String[] permissions = {
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
            };

            boolean granted = true;
            for(String permission : permissions) {
                if(ActivityCompat.checkSelfPermission(this, permission)!= PackageManager.PERMISSION_GRANTED){
                    granted = false;
                    break;
                }
            }

            if(granted==false){
                requestPermissions(permissions, 0x01);
            }else{
                initLocation();
            }
        }else{
            initLocation();
        }
    }

    @SuppressLint("MissingPermission")
    private void initLocation(){
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                5000, 5, new LocationListener() {
                    @Override
                    public void onLocationChanged(@NonNull Location location) {
                        if(location!=null) {
                            longitudeText.setText(Double.toString(location.getLongitude()));
                            latitudeText.setText(Double.toString(location.getLatitude()));
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}